/**
 * JavaScript
 *
 * Copyright (C)
 * This program is free software. You can redistribute it and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation. Either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. Without even the implied warranty
 * of ERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program. Ff not, write to the Free Software
 * Fundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 *
 * @author Bastian Schröder <bs@chemanet.de>
 * @version 2008-04-19
 * @package railusers
 */

/* -------------------------------------- groups -------------------------------------- */

/* Send id to delete via AJAX call. Refresh groups and update inventory innerHTML.
 *
 * @param int id rail_id
 */
function group_delete( id ) {
	if( window.confirm('Die aktuelle Gruppe wird gelöscht.') )
		ajax_call( 'group_delete&group='+id, {load: function( ajax_response ) {
			// on error response
			if( ajax_response.type == 'ajax_error_response' ){
				alert("Die Gruppe konnte nicht gelöscht werden:\n" + ajax_response.exceptions[0] );
				return
			}

			groups_refresh();
			document.getElementById('inventory').innerHTML = '';
		}} );
}


/* Load a rail_group via AJAX call.
 *
 * @param int id rail_id
 */
function group_open( id ) {
	ajax_call( 'group&group='+id, {load: function( ajax_response ) {
		document.getElementById('inventory').innerHTML = ajax_response.html_code;
		dojo.parser.parse( document.getElementById('inventory') );
	}} );
}


/* Update current group, and refresh groups. Send group_form formular via AJAX call.
 *
 * @param int id rail_id
 */
function group_update( id ) {
	ajax_call_post( 'group_form', 'group_update&group='+id, {load: function( ajax_response ) {
		groups_refresh();
	}} );
}


/* Remove marked rail_user from current group. Send group_form formular via AJAX call.
 *
 * @param int id rail_id
 */
function group_user_delete( id ){
	var user = document.getElementsByName('group_members')[0].value;
	if( !user ) return;

	if( window.confirm('Der markierte Benutzer wird entfernt.') )
		ajax_call_post( 'group_form', 'group_user_delete&group='+id, {load: function( ajax_response ) {
			group_open(id);
		}} );
}


/* Add marked rail_user to current group. Send group_form formular via AJAX call.
 *
 * @param int id rail_id
 */
function group_user_add( id ){
	alert( document.getElementsByName('group_user_add')[0].value );

	if( !document.getElementsByName('group_user_add')[0].value )
		return;

		alert(2);

	ajax_call_post( 'group_form', 'group_user_add&group='+id, {load: function( ajax_response ) {
		group_open(id);
	}} );
}


/* Get all rail_group's via AJAX call. Set groups innerHTML and start dojo.parser for groups.
 *
 */
function groups_refresh () {
	ajax_call( 'groups', {load: function( ajax_response ) {
		document.getElementById('groups').innerHTML = ajax_response.html_code;
		dojo.parser.parse( document.getElementById('groups') );
	}} );
}


/* --------------------------------------- users -------------------------------------- */

function user_add () {
	ajax_call_post( 'add_user_form', 'user_add', {load: function( ajax_response ) {
		var el = document.getElementById('inventory');
		el.innerHTML = ajax_response.html_code;
		dojo.parser.parse( el );
	}} );
}


/* Send id to delete via AJAX call. Refresh users and update inventory innerHTML.
 *
 * @param int id rail_id
 */
function user_delete( id ) {
	if( window.confirm('Der aktuelle Benutzer wird gelöscht.') )
		ajax_call( 'user_delete&user='+id, {load: function( ajax_response ) {
			// on error response
			if( ajax_response.type == 'ajax_error_response' ){
				alert("Der Benutzer konnte nicht gelöscht werden:\n" + ajax_response.exceptions[0] );
				return
			}

			users_refresh();
			document.getElementById('inventory').innerHTML = '';
		}} );
}


/* Load a rail_user via AJAX call.
 *
 * @param int id rail_id
 */
function user_open( id ) {
	ajax_call( 'user&user='+id, {load: function( ajax_response ) {
		dijit.byId('inventory').destroyChildren();
		document.getElementById('inventory').innerHTML = ajax_response.html_code;
		dojo.parser.parse( document.getElementById('inventory') );
	}} );
}


/* Remove current user from marked rail_group. Send user_form formular via AJAX call.
 *
 * @param int id rail_id
 */
function user_group_delete( id ){
	var group = document.getElementsByName('user_memberships')[0].value;
	if( !group ) return;
	// return if trying to delete the primary group
	if( group == 'primary' ){
		alert('Die Primärgruppe kann nicht entfernt werden.');
		return;
	}
	// Return if trying to delete a not writeable group
	if( group.substring(0, 12) == 'notreadable_' ){
		alert('Die Gruppe kann nicht entfernt werden.\nSie haben keine Schreibrechte für die Gruppe.');
		return;
	}

	if( window.confirm('Die markierte Gruppe wird entfernt.') )
		ajax_call_post( 'user_form', 'user_group_delete&user='+id, {load: function( ajax_response ) {
			user_open(id);
		}} );
}


/* Add current user to marked rail_group. Send user_form formular via AJAX call.
 *
 * @param int id rail_id
 */
function user_group_add( id ){
	if( !document.getElementsByName('user_group_add')[0].value )
		return;

	ajax_call_post( 'user_form', 'user_group_add&user='+id, {load: function( ajax_response ) {
		user_open(id);
	}} );
}


/* Update current user, and refresh users. Send user_form formular via AJAX call.
 *
 * @param int id rail_id
 */
function user_update( id ) {
	ajax_call_post( 'user_form', 'user_update&user='+id, {load: function( ajax_response ) {
		users_refresh();
	}} );
}


/* Get all rail_user's via AJAX call. Set users innerHTML and start dojo.parser for users.
 *
 */
function users_refresh () {
	ajax_call( 'users', {load: function( ajax_response ) {
		document.getElementById('users').innerHTML = ajax_response.html_code;
		dojo.parser.parse( document.getElementById('users') );
	}} );
}